package com.mx.zb.mng.job;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.RestInter;
@Component
public class tenantSchedule {

	@Autowired
	TenantMapper tenantMapper;
	
	@Autowired
	RestInter    restInter;
	
	@Value("${custom.tenant.host}")
	private String tenantHost;
	
	@Value("${custom.tenant.host1}")
	private String tenantHost04;
	
	@Value("${custom.tenant.setTenantLapsePath}")
	private String setTenantLapsePath;

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	// 查看商户是否过期
	@Transactional
	@Scheduled(cron = "${custom.schedule.cron.tenantStatus}")
	public void setTenantLapse() {
		log.debug("=======每月一号查看商户是否过期true =设置为过期失效========");
		Tenant condition = new Tenant();
		List<Tenant> list = tenantMapper.findAll();
		for (Tenant tenant : list) {
			condition = tenantMapper.selectByPrimaryKey(tenant.getId());
			Date stopUseDate = condition.getStopUseDate();
			Calendar ca = Calendar.getInstance();
			Date currDate = ca.getTime();
			if (currDate.getTime() - stopUseDate.getTime() > 0) { // 到期了
				Tenant ta = new Tenant();
				ta.setIsActive("2");// 表示过期了
				ta.setId(tenant.getId());
				tenantMapper.updateByPrimaryKeySelective(ta);
				JSONObject param = new JSONObject();
				param.put("tenant", condition);
				if("技师服务".equals(condition.getTemplateId())) {
					JSONObject jsparam =restInter.postJson(tenantHost04+setTenantLapsePath, param);
					if (!"1".equals(jsparam.getString("status"))) {
						log.error("技师服务商户过期修改失败!"+tenant.getId());
					}
				}else {
					// 更新 商户平台商户状态
				JSONObject jsparam =restInter.postJson(tenantHost+setTenantLapsePath, param);
				if (!"1".equals(jsparam.getString("status"))) {
					log.error("零售电商商户过期修改失败!"+tenant.getId());
				}
				}
			}

		}
	}
}
